/*
 Copyright (C) 2012  Nikos Siatras
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

package ncore_tcp_port_forward.Core;

import Extasys.DataFrame;
import Extasys.Network.TCP.Client.Connectors.TCPConnector;
import Extasys.Network.TCP.Client.ExtasysTCPClient;
import Extasys.Network.TCP.Server.Listener.TCPClientConnection;
import java.net.InetAddress;

/**
 *
 * @author Nikos Siatras
 */
public class TCPClient extends ExtasysTCPClient
{

    private TCPClientConnection fMyClientConnection;
    private boolean fIsStarted = false;

    public TCPClient(TCPClientConnection myClientConnection, String name, String description, InetAddress remoteHostIP, int remoteHostPort, int corePoolSize, int maximumPoolSize)
    {
        super(name, description, corePoolSize, maximumPoolSize);

        fMyClientConnection = myClientConnection;

        try
        {
            super.AddConnector(name, remoteHostIP, remoteHostPort, 8192);
        }
        catch (Exception ex)
        {
        }
    }

    @Override
    public void OnDataReceive(TCPConnector connector, DataFrame data)
    {
        try
        {
            fMyClientConnection.SendData(data.getBytes(), 0, data.getLength());
        }
        catch (Exception ex)
        {
        }
    }

    @Override
    public void OnConnect(TCPConnector connector)
    {
        fIsStarted = true;
    }

    @Override
    public void OnDisconnect(TCPConnector connector)
    {
        fMyClientConnection.DisconnectMe();
        fIsStarted = false;
    }

    @Override
    public void Start() throws Exception
    {
        super.Start();
    }

    @Override
    public void Stop()
    {
        fIsStarted = false;
        super.Stop();
    }

    public boolean isStarted()
    {
        return fIsStarted;
    }
}
